package dataStorage;

import inputs.Input;

/**
 * DataBank Class
 * 
 * Used to store an array, of a given fixed size, of Integers.
 * 
 * Has a getter and setter that will allow access to each index.
 * Setters will take an Integer, but there is also an overload for taking Inputs. When taking an Input, it will store the current value, but will not update when that value changes.
 * 
 * There is also a getNext() and setNext() which will loop through the indices of the DataBank. 
 * getNext() will always reset to the beginning after going off the end.
 * setNext() will reset if the loopSet boolean is set to true in the constructor. Otherwise, it will stop setting after it gets to the end.
 * 
 * @author Dylan James
 *
 */
public class DataBank {
	private Integer[] data;
	
	private boolean loopSet;
	
	private int nextSetIndex = 0;
	private int nextGetIndex = 0;
	
	/**
	 * Constructs a new DataBank with a given size and loop boolean.
	 * @param size The number of indices this DataBank will contain.
	 * @param loopSet True if setNext() should reset to the beginning after reaching the end.
	 */
	public DataBank(int size, boolean loopSet)
	{
		this.loopSet = loopSet;
		
		data = new Integer[size];
		
		for (int i = 0; i < size; i++)
		{
			data[i] = null;
		}
	}
	
	/**
	 * Sets the given index to the given integer value.
	 * @param index The index to set.
	 * @param value The value to set it to.
	 */
	public void set(int index, Integer value)
	{
		if (index >= 0 && index < data.length)
			data[index] = value;
	}
	
	/**
	 * Sets the given index to the value in the given input.
	 * Does not update each tick.
	 * @param index The index to set.
	 * @param value The Input whose value to set it to.
	 */
	public void set(int index, Input value)
	{
		set(index, value.getValue());
	}
	
	/**
	 * Sets the next index in the DataBank to the given value.
	 * Iterates from the first index to the last, and resets if loopSet was true in the constructor.
	 * @param value The value to set the index to.
	 * @return Returns the index that was set, or -1 if none was set because the end was reach and loopSet was false.
	 */
	public int setNext(Integer value)
	{
		int setIndex = nextSetIndex;
		
		if (setIndex < data.length)
			set(setIndex, value);
		else
		{
			if (loopSet)
			{
				setIndex = 0;
				set(setIndex, value);
			}
			else
			{
				return -1;
			}
		}
		
		nextSetIndex = setIndex + 1;
		
		return setIndex;
	}
	
	/**
	 * Sets the next index in the DataBank to the value of the given Input.
	 * Iterates from the first index to the last, and resets if loopSet was true in the constructor.
	 * @param value The Input whose value to set the index to.
	 * @return Returns the index that was set, or -1 if none was set because the end was reach and loopSet was false.
	 */
	public int setNext(Input value)
	{
		return setNext(value.getValue());
	}
	
	/**
	 * Gets the value of the given index.
	 * @param index The index to get the value of.
	 * @return The Integer value that was stored in the index.
	 */
	public Integer get(int index)
	{
		if (index >= 0 && index < data.length)
			return data[index];
		
		return null;
	}
	
	/**
	 * Gets the value of the next index in the DataBank.
	 * Iterates from the first to last, and then loops back to the beginning.
	 * @return The value of the next index in the DataBank.
	 */
	public Integer getNext()
	{
		Integer ret = get(nextGetIndex);
		
		nextGetIndex++;
		if (nextGetIndex >= data.length)
			nextGetIndex = 0;
		
		
		return ret;
	}
}
